package com.xuwei.treeslidemenu;

/**
 * 仿js可滑动树菜单
 * 
 * @author 许伟
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TreeSlideMenu extends JPanel {

    private static final long serialVersionUID = 1L;

    private JButton[] btn = null;

    private JPanel pNorth, pSouth, subP, subMenuContainer;

    private JScrollPane pCenter;

    private static boolean expand = true;// 是否展开子菜单

    private static Integer activeGroup = null;// 当前主菜单是否激活

    private Menu[] menu = null;// 主菜单

    private int subMenuWidth = 400;// 子菜单宽度

    private int subMenuHeight = 30;// 子菜单高度

    public int getSubMenuWidth() {
        return subMenuWidth;
    }

    public void setSubMenuWidth(int subMenuWidth) {
        this.subMenuWidth = subMenuWidth;
    }

    public int getSubMenuHeight() {
        return subMenuHeight;
    }

    // 设置子菜单高度
    public void setSubMenuHeight(int subMenuHeight) {
        this.subMenuHeight = subMenuHeight;
    }

    /**
     * 
     * @param menu
     *            菜单实体
     * @param subMenuWidth
     *            子菜单宽度
     * @param subMenuHeight
     *            子菜单高度
     */
    public TreeSlideMenu(Menu[] menu, int subMenuWidth, int subMenuHeight) {
        // 初始化参数
        this.setSubMenuWidth(subMenuWidth);
        this.setSubMenuHeight(subMenuHeight);

        // 初始化组件
        initComs(menu);

        // 添加组件
        addComs(menu);

        // 默认展开第一个主菜单
        expandMenu(0);

    }

    // 添加组件
    private void addComs(Menu[] menu) {
        for (int i = 0; i < menu.length; i++) {
            btn[i] = new JButton(menu[i].getName());
            btn[i].setBackground(menu[i].getBackgroundColor());
            btn[i].setActionCommand(i + "");

            // 给主菜单绑定事件
            if (menu[i].getSubMenus() != null) {
                btn[i].addActionListener(new ActionHandler());
            } else {
                ActionListener al = menu[i].getActionListener();
                if (al != null)
                    btn[i].addActionListener(al);
            }

            pNorth.add(btn[i]);
        }

        pCenter = new JScrollPane(subP);
        pCenter.setBackground(Color.WHITE);

        this.setLayout(new BorderLayout());
        this.add(pNorth, "North");
        this.add(pCenter, "Center");
        this.add(pSouth, "South");
    }

    // 初始化组件
    private void initComs(Menu[] menu) {
        this.menu = menu;
        pNorth = new JPanel();
        pNorth.setBackground(Color.WHITE);
        pNorth.setLayout(new GridLayout(menu.length, 1));
        pSouth = new JPanel();
        pSouth.setBackground(Color.WHITE);
        pSouth.setLayout(new GridLayout(2, 1));
        subP = new JPanel();
        subP.setBackground(Color.WHITE);
        subP.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        subMenuContainer = new JPanel();
        subMenuContainer.setBackground(Color.WHITE);
        subMenuContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        subP.add(subMenuContainer);
        btn = new JButton[menu.length];
    }

    // 折叠菜单
    private void collapseMenus(int n) {
        pNorth.setLayout(new GridLayout(menu.length, 1));
        // 把center中的子菜单移除
        subMenuContainer.removeAll();
        // 把south上的主菜单移到north上
        for (int i = n; i < menu.length - 1; i++) {
            pSouth.remove(btn[i + 1]);
            pNorth.add(btn[i + 1]);
        }
        pCenter.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        pCenter.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        validate();
        this.repaint();
        expand = true;
        activeGroup = n;
    }

    // 展开菜单
    private void expandMenu(int n) {
        pNorth.setLayout(new GridLayout(n + 1, 1));
        pSouth.setLayout(new GridLayout(menu.length - n - 1, 1));
        // 把当前主菜单后面的菜单移到south面板
        for (int i = n; i < menu.length - 1; i++) {
            pNorth.remove(btn[i + 1]);
            pSouth.add(btn[i + 1]);
        }
        // 向center面板中添加子菜单
        Menu[] subMenu = menu[n].getSubMenus();
        subMenuContainer.setPreferredSize(new Dimension(this.getSubMenuWidth(), subMenuHeight * subMenu.length));
        for (int i = 0; i < subMenu.length; i++) {
            JButton btn2 = new JButton(subMenu[i].getName());
            btn2.setBackground(subMenu[i].getBackgroundColor());

            // 给子菜单绑定事件
            ActionListener e = subMenu[i].getActionListener();
            if (e != null) {
                btn2.addActionListener(e);
            }

            subMenuContainer.setLayout(new GridLayout(subMenu.length, 1));
            subMenuContainer.add(btn2);
        }

        pCenter.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        pCenter.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        validate();
        this.repaint();
        expand = false;
        activeGroup = n;
    }

    // 事件处理类
    private class ActionHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int n = Integer.parseInt(e.getActionCommand());
            if (activeGroup != null) {
                if (activeGroup != n) {
                    collapseMenus(activeGroup);
                    expandMenu(n);
                } else {
                    if (expand) {// 展开
                        expandMenu(n);
                    } else {// 折叠
                        collapseMenus(n);
                    }
                }
            } else {
                if (expand) {// 展开
                    expandMenu(n);
                } else {// 折叠
                    collapseMenus(n);
                }
            }
        }

    }

}
